package net.wicp.tams.common.binlog.dump.bean;

import lombok.Builder;
import lombok.Data;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.binlog.dump.DumpGroup;
import net.wicp.tams.common.binlog.dump.listener.IBusiFilter;
import net.wicp.tams.common.binlog.dump.listener.IBusiSender;
import net.wicp.tams.common.constant.dbType.BinlogType;

@Data
@Builder
public class Dump {
	private String id;
	private String db;
	private String tb;
	private String[] primarys;
	private String[] needCols;// 需要导出的列
	private BinlogType[] needColTypes;// 需要导出列的类型
	private int numDuan;
	private String wheresql;// where语句
	private String startId;// 从哪个ID开始导入
	private Long numDump;// 导多少记录数
	private IBusiFilter<DumpEvent> busiFilter;
	private IBusiSender<DumpEvent> busiSender;

	private DumpGroup metric;

	private volatile String lastId;

	public String packFromstr() {
		if (StringUtil.isNotNull(this.wheresql)) {
			this.wheresql = StringUtil.trimSpace(this.wheresql);
			if (!this.getWheresql().substring(0, 5).equalsIgnoreCase("where")) {
				this.wheresql = "where " + this.wheresql;
			}
		}
		String fromstr = String.format("from %s %s", String.format("%s.%s", db, tb),
				StringUtil.isNull(this.getWheresql()) ? "where 1=1 " : this.getWheresql());
		return fromstr;
	}

	public static BinlogType[] convertColumnType(String[] cosTypes) {
		BinlogType[] rettypes = new BinlogType[cosTypes.length];
		for (int i = 0; i < rettypes.length; i++) {
			rettypes[i] = BinlogType.getByName(cosTypes[i]);
		}
		return rettypes;
	}

	/*
	 * @SuppressWarnings("unchecked") public IBusiSender<DumpEvent> getSender() { if
	 * (this.sender == null) { try { IBusiSender<DumpEvent> newInstance =
	 * (IBusiSender<DumpEvent>)
	 * busiSendClass.getConstructor(Dump.class).newInstance(this); this.sender =
	 * newInstance;
	 * 
	 * } catch (Exception e) { throw new RuntimeException("没有实例", e); } } return
	 * this.sender; }
	 */
}
